package com.un.ebs.sys.service.impl;

import com.un.ebs.core.exception.BusinessException;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author zc
 * @ClassName DbHandler
 * @Description
 * @Date 2021/1/14
 */
public class DbHandler {


    /**
     * 获取连接的公共方法
     *
     * @return
     */
   /* public static Connection getConnection() {

        try {
            Class.forName(DRIVER);
            return DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;

    }
*/

    /**
     * 关闭Connection
     *
     * @param connection
     */
    public static void closeConnection(Connection connection) {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭Statement
     *
     * @param statement
     */
    public static void closeStatement(Statement statement) {
        try {
            if (statement != null) {
                statement.close();
            }
        } catch (SQLException e) {
            throw new BusinessException(e.getMessage());
        }
    }

    /**
     * 关闭ResultSet
     *
     * @param resultSet
     */
    public static void closeresultset(ResultSet resultSet) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (SQLException e) {
            throw new BusinessException(e.getMessage());
        }
    }

    /**
     * 关闭ResultSet, Statement, Connection
     *
     * @param closeables
     */
    public static void closeAll(AutoCloseable... closeables) {
        for (AutoCloseable autoCloseable : closeables) {
            try {
                if (autoCloseable != null) {
                    autoCloseable.close();
                }
            } catch (Exception e) {
                throw new BusinessException(e.getMessage());
            }
        }
    }


    public List<Map<String, Object>> executeQuery(String sql, Connection conn) {
        List<Map<String, Object>> list = new ArrayList<>();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn.setAutoCommit(false);
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            ResultSetMetaData md = rs.getMetaData();
            int columnCount = md.getColumnCount();
            while (rs.next()) {
                Map<String, Object> rowData = new HashMap();
                for (int i = 1; i <= columnCount; i++) {
                    rowData.put(md.getColumnName(i), rs.getObject(i));
                }
                list.add(rowData);
            }
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
            throw new BusinessException(e.getMessage());
        } finally {
            closeConnection(conn);
            closeresultset(rs);
            closeStatement(ps);
        }
        return list;
    }

    public int executeUpdate(String sql, Connection conn) {
        PreparedStatement ps = null;
        try {
            conn.setAutoCommit(false);
            ps = conn.prepareStatement(sql);
            int row = ps.executeUpdate();
            conn.commit();
            return row;
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
            throw new BusinessException(e.getMessage());
        } finally {
            closeConnection(conn);
            closeStatement(ps);
        }
    }
}
